package lang;

import org.nutz.lang.Lang;

public class ExceptionPractice {

	//抛出的这种运行时异常不用显式的用throws声明，因此调用函数时也不需要显式的进行try..catch操作。
	static void throwRunTime()
	{
		throw Lang.makeThrow("error from %d [%s]", 1024,"1024端口出错");
	}
		
	//加入Exception参数时将不是包装成运行时异常。在编译器下你必须显式的抛出该自定义异常。
	static void throwMyException() throws MyException
	{
		throw Lang.makeThrow(MyException.class,"%s","nothing");
	}
	
	//简易的抛出一个未实现异常
	static void throwLazy()
	{
		throw Lang.noImplement();
	}
	
	//抛出多个异常
	static void throwMany()
	{
		MyException e1=new MyException();
		ArrayIndexOutOfBoundsException e2=new ArrayIndexOutOfBoundsException();
		StackOverflowError e3=new StackOverflowError();
		throw Lang.comboThrow(e1,e2,e3);
	}
	
	//在catch中再次抛出的时候将其包装成运行时异常
	static void wrapThrow()
	{
		try{
			throw new MyException();
		}catch (MyException e) {
			throw Lang.wrapThrow(e);
		}
	}
	
	//将捕获的异常包装成为其他类型异常
	static void wrapCustomThrow()
	{
		try{
			throw new MyException();
		}catch (MyException e) {
			throw Lang.wrapThrow(e,NullPointerException.class);
		}
	}
	
	public static void main(String[] args)  {
//		wrapThrow();
		try{
			throwMany();
//			wrapCustomThrow();
//			throwLazy();
//			throwMyException();
		//throwRunTime();
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
	}
}


